var editMatchProject = {
    // 参数信息
    params:{
        parentCustomerCode:'',
        customerCode:'',
        operateUserId:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID,
        operateUserName:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name
    },
    submitParams:[],
    init:function(){
        if(getQueryString("channelId")){
            this.params.parentCustomerCode = getQueryString("channelId");
        }
        if(getQueryString("customerCode")){
            this.params.customerCode = getQueryString("customerCode");
        }
        this.getDetailInfo(this.params);
        this.addClick();
    },
    addClick:function(){
        // 删除订单数据
        $('#orderLists').delegate('img','click',function(){
            var that = this;
            layer.confirm('请确认要删除该条数据，删除后则无法对应项目关系，将影响订单生产!',{
                btn: ['确认', '取消'], //按钮
                title:'删除提示',
                offset:"t"
            },function(index){
                $(that).parent().parent().remove();
                var id = $(that).attr('data-id');
                editMatchProject.submitParams.push(id)
                layer.close(index)
            })
        })

        // 确认
        $('#sureButton').unbind().on('click',function(){
            editMatchProject.params.ids = editMatchProject.submitParams;
            if(editMatchProject.submitParams.length<=0){
                layer.msg('请选择你要删除地址信息',{offset:'t'});
                return
            }
            editMatchProject.submitDeleteInfo()
        });
        // 取消
        $('#cancelButton').unbind().on('click',function(){
            parent.layer.closeAll();
        })
    },
    // 项目匹配详情接口
    getDetailInfo:function(params){
        var successData = function(data){
            if(data.errorCode == 0){
                var result = data.data;
                if(result){
                    var resultData = {
                        channelName:result.parentCustomerName?result.parentCustomerName:'',//渠道名称
                        channelOrderId:result.parentCustomerCode?result.parentCustomerCode:'',//渠道id
                        customerName:result.customerName?result.customerName:'',//客户名称
                        customerId:result.customerId?result.customerId:'',//客户id
                        addressLists:result.addressMapList
                    };
                    editMatchProject.renderDetail(resultData)
                }
            }
        }

        return web.doAjax(root.channelManagement.queryAddressConfigDetails, 'post', params, successData, 1);

    },
    renderDetail:function(data){
        $('#channelName').text(data.channelName); //渠道名称
        $('#channelOrderId').text(data.channelOrderId);//渠道ID
        $('#customerName').text(data.customerName);//客户名称
        $('#customerId').text(data.customerId);//客户id

        //地址列表渲染
        var addressLists = data.addressLists;
        if(addressLists.length > 0){
            var str = '';
            addressLists.forEach(function(item,index){
                var receiveAddress = item.provinceName + item.cityName + item.areaName + item.townName + item.detailAddress;
                str+='<div style="overflow: hidden">' +
                    '                <div style="float: left;margin-right: 10px">地址'+(index+1)+': </div>' +
                    '                <div style="float: left">' +
                    '                    <span>'+receiveAddress+'</span>' +
                    '                    <img  data-id="'+item.id+'" src="../../../img/fixnews_up.png" alt="" style="cursor: pointer;vertical-align: top">' +
                    '                </div>' +
                    '            </div>'
            })
            $('#orderLists').html(str)

        }

    },
    //提交/删除接口
    submitDeleteInfo:function(){
        var successData = function(data){
            if(data.errorCode == 0){
                layer.msg('提交成功',{offset:'t'});
                setTimeout(function () {
                    var index = parent.location.reload();
                    parent.layer.close(index)
                }, 1000);
            }
        }
        return web.doAjax(root.channelManagement.deleteAddressConfig, 'post', editMatchProject.params, successData, 1);
    }

}
editMatchProject.init();